package com.eascs.spider.ant.common.kafka;

import java.util.Arrays;
import java.util.Properties;

import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KafkaConsumer {
	
	private static Logger logger = LoggerFactory.getLogger(KafkaConsumer.class);
	
	private static Consumer<String, Object> consumer;
	
	public synchronized static void init(){
		if(consumer !=null)
			return;
		Properties props = new Properties();
        props.put("bootstrap.servers", "172.29.13.121:9092");
        props.put("group.id", "GroupA");
        props.put("enable.auto.commit", "true");
        props.put("auto.commit.interval.ms", "1000");
        props.put("session.timeout.ms", "30000");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", DecodeingKafka.class.getName());
        consumer = new org.apache.kafka.clients.consumer.KafkaConsumer<String, Object>(props);
        pull();
	}
	
	private static void pull(){
		consumer.subscribe(Arrays.asList(KafkaProducer.TOPIC));
        while(true){
        	ConsumerRecords<String, Object> records = consumer.poll(100);
        	for (ConsumerRecord<String, Object> record : records){
        			
                logger.info("offset={}, value:{}" , record.offset() ,record.value());
            }
        }
	}
	
	public static void main(String[] args) {
		
        
	}
}
